package com.iiapk.grid.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import com.iiapk.gridnew.domain.Article;
import com.iiapk.gridnew.domain.ColumnType;

public class SQLiteHelper extends SQLiteOpenHelper {

	protected static String DATABASE_NAME = "gridnews.db";
	protected static int DATABASE_VERSION = 4;
	private volatile static SQLiteHelper mHelper = null;
	
	private SQLiteHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	public static SQLiteHelper getInstance(Context ctx) {
		if(null == mHelper){
			synchronized(SQLiteHelper.class) {
				if(null == mHelper) {
					mHelper = new SQLiteHelper(ctx, DATABASE_NAME, null, DATABASE_VERSION);
				}
			}
		}
		return mHelper;
	}
	
	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table if not exists "+ Article.TABLE_NAME +
			"("+Article.COLUMN_ID +" nvarchar(10),"+
			Article.COLUMN_TITLE +" nvarchar(40) NOT NULL,"+
			Article.COLUMN_CONTENT +" text,"+
			Article.COLUMN_TIME +" nvarchar(20),"+
			Article.COLUMN_BELONGTO +" nvarchar(10),"+
			Article.COLUMN_IAMGE +" nvarchar(40));");
		db.execSQL("create table if not exists "+ ColumnType.TABLE_NAME +
			"("+ColumnType.COLUMN_ID +" nvarchar(10),"+
			ColumnType.COLUMN_TITLE +" nvarchar(20) NOT NULL,"+
			ColumnType.COLUMN_ORDER +" INTEGER,"+
			ColumnType.COLUMN_ICON +" nvarchar(40));");
		
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('1', '游戏天地', 'grid_item_game',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('2', '环球时报', 'grid_item_world',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('3', 'Steam', 'grid_item_steam',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('4', '电影资讯', 'grid_item_film',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('5', '汽车世界', 'grid_item_car',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('6', '游戏天地', 'grid_item_game',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('7', '环球时报', 'grid_item_world',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('8', 'Steam', 'grid_item_steam',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('9', '电影资讯', 'grid_item_film',9999);");
		db.execSQL("INSERT INTO ColumnType(id,title,icon,idx) VALUES('10', '汽车世界', 'grid_item_car',9999);");
		
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('1','美貌的五个优势和劣势'," +
			"'http://www.baidu.com','','2011-11-24 10:36','2');");
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('2','买器官可以合法化吗?'," +
			"'http://www.google.cn','','2011-11-24 10:36','2');");
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('3','葡萄美酒纸瓶装'," +
			"'http://www.baidu.com','','2011-11-24 10:36','2');");
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('4','王者之剑'," +
			"'http://www.google.cn','','2011-11-24 10:36','2');");
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('5','胎儿也会保护妈妈'," +
			"'http://www.baidu.com','','2011-11-24 10:36','2');");
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('6','最轻的材料什么样?'," +
			"'http://www.google.cn','','2011-11-24 10:36','2');");
		db.execSQL("INSERT INTO Article(id,title,content,image,createTime,belongTo)VALUES('7','牛仔裤，越脏越好?'," +
			"'http://www.baidu.com','','2011-11-24 10:36','2');");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists "+ ColumnType.TABLE_NAME);
		db.execSQL("drop table if exists "+ Article.TABLE_NAME);
	    onCreate(db);
	}

	@Override
	public synchronized void close() {
		super.close();
		mHelper = null;
	}
}
